﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiografController;
using BiografController.ServiceReference;
using System.Security.Cryptography;

namespace BiografGUI
{
    public partial class KundeGUI : Form
    {
        
        private KundeController kCtr;
        public KundeGUI()
        {
            kCtr = new KundeController();
            InitializeComponent();
            searchButton.Enabled = false;
        }

        private void InsertKunde()
        {
            Kunde k = new Kunde();
            k.TelefonNr = Convert.ToInt32(tlfTxtBox.Text);
            k.Navn = navnTxtBox.Text;
            k.Email = emailTxtBox.Text;
            int telefon = Convert.ToInt32(tlfTxtBox.Text);
            string navn = navnTxtBox.Text;
            string email = emailTxtBox.Text;
            string password = k.TelefonNr.ToString();
            string hashedPassword = Convert.ToBase64String(System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(password)));
            
            try 
	        {	        
		        kCtr.InsertKunde(k.TelefonNr, k.Navn, k.Email, hashedPassword);
                MessageBox.Show(string.Format("{0} \n{1}\n{2} er oprettet.", k.Navn, k.TelefonNr, k.Email));
                this.Hide();
                
	        }
	        catch (Exception)
	        {
                MessageBox.Show("Fejl i indsætning af kunde");
	        }
        }

        private void Clear()
        {
            navnTxtBox.Text = "";
            tlfTxtBox.Text = "";
            emailTxtBox.Text = "";
            statusLabel.Text = "Status: ";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InsertKunde();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Clear();
        }


        private void ValidateTlfTextBox()
        {
            if (!tlfTxtBox.Equals("") && tlfTxtBox.Text.Length == 8)
            {
                searchButton.Enabled = true;
            }
            else
            {
                searchButton.Enabled = false;
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            GetKunde();
        }

        private void GetKunde()
        {
            try
            {
                if (kCtr.GetEnKunde(Convert.ToInt32(tlfTxtBox.Text)).Navn.Equals(""))
                {
                    statusLabel.Text = "Kunden eksisterer ikke";
                }
                else
                {
                    Kunde k = new Kunde();
                    k = kCtr.GetEnKunde(Convert.ToInt32(tlfTxtBox.Text));
                    navnTxtBox.Text = k.Navn;
                    emailTxtBox.Text = k.Email;
                    statusLabel.Text = "Status: Kunden er hentet!";
                }
            }
            catch (Exception)
            {
                Clear();
                MessageBox.Show("Kunden eksisterer ikke");
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ValidateTlfTextBox();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int tlf = Convert.ToInt32(tlfTxtBox.Text);
                kCtr.EditKunde(tlf, navnTxtBox.Text, emailTxtBox.Text);
                Clear();
                statusLabel.Text = "Status: " + kCtr.GetEnKunde(tlf).Navn + " er opdateret";
                MessageBox.Show(string.Format("{0} \n{1}\n{2} er opdateret.",kCtr.GetEnKunde(tlf).Navn, kCtr.GetEnKunde(tlf).TelefonNr, kCtr.GetEnKunde(tlf).Email));
                //kCtr.GetEnKunde(tlf).Navn + " er opdateret."
                this.Hide();
            }
            catch (Exception)
            {
                MessageBox.Show("Fejl i opdatering af kunde");
            }
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                GetKunde();
            }
        }

    }
}
